package com.futvan.z.httpservices.shop;
import com.futvan.z.erp.customer.crm_customer;
import com.futvan.z.framework.core.SuperAction;
import com.futvan.z.framework.common.bean.Result;
import com.futvan.z.framework.common.bean.Code;
import com.futvan.z.framework.core.z;
import com.futvan.z.framework.util.DateUtil;
import com.futvan.z.framework.util.enums.ZxUtil;
import com.futvan.z.shop.shop.s_shop;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.util.HashMap;
/**
 * 保存直销会员
 */
@Transactional
@RestController
public class SaveZXCustomerHttpService extends SuperAction{

	@RequestMapping(value="/SaveZXCustomer")
	public @ResponseBody Result SaveZXCustomer(@RequestParam HashMap<String, String> bean) throws Exception {
		Result result = new Result();
		String customid = bean.get("customid");
		//判读客户是否存在
		int n = sqlSession.selectOne("selectoneint","select count(*) from crm_customer where zid = '"+customid+"'");
		if(n<=0){
			Result r = ZxUtil.getZxCustomer(customid);
			if(r.ifSuccess()){
				HashMap m = (HashMap) r.getData();

				String nick = String.valueOf(m.get("nick"));
				String id_no = String.valueOf(m.get("id_no"));
				String mobile = String.valueOf(m.get("mobile"));
				String password_login = String.valueOf(m.get("password_login"));
				String password_pay = String.valueOf(m.get("password_pay"));
				String real_name = String.valueOf(m.get("real_name"));
				String share_nick = String.valueOf(m.get("share_nick"));
				String sys_masterid = String.valueOf(m.get("sys_masterid"));
				String dtype = String.valueOf(m.get("dtype"));
				String groupId = String.valueOf(m.get("groupId"));
				String is_display_acc2_button = "0";
				if("1".equals(dtype)){
					is_display_acc2_button = "0";
				}else if("0".equals(dtype) && "6".equals(groupId)){
					is_display_acc2_button = "1";
				}
				String jxsMember = String.valueOf(m.get("jxsMember"));
				String sign_date = String.valueOf(m.get("sign_date"));

				String level = "0";
				String level1_createtime = "";
				if("2".equals(jxsMember)){
					level = "1";
					if(z.isNotNull(sign_date)){
						level1_createtime = sign_date;
					}else{
						level1_createtime = DateUtil.getDateTime();
					}
				}
				String joinTime = String.valueOf(m.get("joinTime"));
				String end_time = String.valueOf(m.get("end_time"));//有效期

				//保存客户信息
				crm_customer c = new crm_customer();
				c.setZid(nick);
				c.setNumber(nick);
				c.setUsername(nick);
				c.setNickname(real_name);
				c.setName(real_name);
				c.setTel(mobile);
				c.setId_number(id_no);
				c.setPassword_login(password_login);
				c.setPassword_pay(password_pay);
				c.setParentid(share_nick);
				c.setIs_enabled("1");
				c.setNode("1");
				c.setLevel(level);
				c.setGroup_num(sys_masterid);
				c.setPassword_changes_num("0");
				c.setIs_display_acc2_button(is_display_acc2_button);
				if(z.isNotNull(level1_createtime)){
					c.setLevel1_createtime(level1_createtime);
				}
				c.setCreate_time(joinTime);
				c.setEnd_time(end_time);//有效期
				int in = sqlSession.insert("crm_customer_insert",c);
				if(in==1){
					s_shop s = new s_shop();
					String shopzid = z.newNumber2();
					s.setZid(shopzid);
					s.setNumber(shopzid);
					s.setCustomid(nick);
					s.setName(real_name);
					s.setTel(mobile);
					s.setWelcome("欢迎光临");
					s.setInfo("欢迎进入"+real_name+"的微店");
					int sin = sqlSession.insert("s_shop_insert",s);
					if(sin==1){
						result.setSuccess();
					}else{
						z.Exception("保存直销会员出错,生成店铺信息返回|n="+sin);
					}
				}else{
					z.Exception("保存直销会员出错|n="+in);
				}
			}else{
				result.setError(r.getMsg());
			}
		}else{
			result.setError(customid+"已导入，无法重新执行");
		}
		return result;
	}
}
